package com.example.webdemo.service.impl;

import com.example.webdemo.dao.UserInfoMapper;
import com.example.webdemo.model.PhoneCode;
import com.example.webdemo.model.UserInfo;
import com.example.webdemo.service.PhoneCodeService;
import com.example.webdemo.service.UserInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author zp
 * @version 1.0
 * @description: UserInfoServiceImpl
 * @date 2023/7/31 19:14
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {
    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private PhoneCodeService phoneCodeService;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return userInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(UserInfo record) {
        return userInfoMapper.insert(record);
    }

    @Override
    public int insertSelective(UserInfo record) {
        return userInfoMapper.insertSelective(record);
    }

    @Override
    public UserInfo selectByPrimaryKey(Integer id) {
        return userInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(UserInfo record) {
        return userInfoMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(UserInfo record) {
        return userInfoMapper.updateByPrimaryKey(record);
    }

    @Override
    public UserInfo selectByPhone(String phone) {
        return userInfoMapper.selectByPhone(phone);
    }

    @Override
    public UserInfo selectByToken(String token) {
        return userInfoMapper.selectByToken(token);
    }

    @Override
    public int updateByPhone(String token, String userPhone) {
        return userInfoMapper.updateByPhone(token,userPhone);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean login(PhoneCode phoneCode) {
        // 作废验证码
        int deCode = updateByPhone(phoneCode.getPhone(),phoneCode.getPhone());
        int upCode = phoneCodeService.deleteByPhone(phoneCode.getPhone());

        return upCode>0 && deCode>0;
    }
}
